package cn.edu.neusoft.food229.model;

import java.util.List;

import cn.edu.neusoft.food229.bean.FoodBean;
import cn.edu.neusoft.food229.bean.ShopBean;
import cn.edu.neusoft.food229.iface.Foodiface;
import cn.edu.neusoft.food229.listener.FoodDetailListener;
import cn.edu.neusoft.food229.listener.FoodListener;
import cn.edu.neusoft.food229.listener.ShopDetailListener;
import cn.edu.neusoft.food229.listener.ShopListener;
import cn.edu.neusoft.food229.service.FoodService;
import cn.edu.neusoft.food229.service.ShopService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static cn.edu.neusoft.food229.Commen.Commen.BASEURL;

public class Foodmodel implements Foodiface{
    private Retrofit retrofit;
    //构造函数
    public Foodmodel(){
        //使用Retrofit--1
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }


    //菜单列表面
    @Override
    public void getResult(String shop_id,final FoodListener listener) {
        FoodService service
           =retrofit.create(FoodService.class);
        Call<List<FoodBean>> call =service.get(shop_id);
        call.enqueue(new Callback<List<FoodBean>>() {
            @Override
            public void onResponse(Call<List<FoodBean>> call, Response<List<FoodBean>> response) {
                if(response.isSuccessful() && response!=null){
                    listener.onResponse((List<FoodBean>)response.body());
                }
                else
                    listener.onFail("on response fail");
            }

            @Override
            public void onFailure(Call<List<FoodBean>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
    //店铺详情页面
    @Override
    public void getResultShopList(final String shop_id, final ShopDetailListener listener) {
        ShopService service
                =retrofit.create(ShopService.class);
        Call<ShopBean> call
                =service.getShopid(shop_id);
        call.enqueue(new Callback<ShopBean>() {
            @Override
            public void onResponse(Call<ShopBean> call, Response<ShopBean> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    listener.onSuccess(response.body(),shop_id);
                    System.out.println("---"+response.body().toString() );
                }else listener.onFail("on response fail");
            }

            @Override
            public void onFailure(Call<ShopBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    @Override
    public void getSearch(final String search, final FoodListener listener) {
        FoodService service=retrofit.create(FoodService.class);
        Call<List<FoodBean>> call=service.searchFood(search);
        call.enqueue(new Callback<List<FoodBean>>() {
            @Override
            public void onResponse(Call<List<FoodBean>> call, Response<List<FoodBean>> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    listener.onResponse(response.body());
                    System.out.println("---"+response.body().toString() );
                }else listener.onFail("on response fail");
            }

            @Override
            public void onFailure(Call<List<FoodBean>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }


}
